<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/20
 * Time: 14:48
 */

namespace app\common\service;


use app\common\access\MyAccess;
use app\common\access\MyService;
use think\Db;

class Team extends MyService
{
    //读取团队成员
    function getList($page=1,$rows=20,$teacherno='%',$teachername='%',$school='',$team='C',$sort='teacherno',$order='')
    {
        $result = ['total' => 0, 'rows' => []];
        $condition = null;
        if ($teacherno != '%') $condition['team.teacherno'] = array('like', $teacherno);
        if ($teachername != '%') $condition['teachers.name'] = array('like', $teachername);
        if ($school != '') $condition['teachers.school'] = $school;
        $condition['team.team']=$team;
        $data=$this->query->table('team')
            ->join('teachers','teachers.teacherno=team.teacherno')
            ->join('schools ',' schools.school=teachers.school')
            ->join('teamtype','teamtype.name=team.team')
            ->page($page,$rows)
            ->field("team.id,team.teacherno teacherno,rtrim(teachers.name) teachername,rtrim(schools.name) schoolname,team.team,rtrim(teamtype.value) teamname,leader")
            ->where($condition)->order($sort.' '.$order)->select();
        $count= $this->query->table('team')
            ->join('teachers','teachers.teacherno=team.teacherno')
            ->where($condition)->count();
        if(is_array($data)&&count($data)>0)
            $result=array('total'=>$count,'rows'=>$data);
        return $result;
    }


    //更新信息
    public function  update($postData){
        $updateRow=0;
        $deleteRow=0;
        $insertRow=0;
        $errorRow=0;
        $info="";
        $status=1;
        //更新部分
        //开始事务

        try {
        if (isset($postData["updated"])) {
            $updated = $postData["updated"];
            $listUpdated = json_decode($updated);
            foreach ($listUpdated as $one) {
                $condition = null;
                $data = null;
                $condition['teacherno']=$one->teacherno;
                $condition['team']=$one->team;
                $data['leader']=(int)$one->leader;
                if(MyAccess::checkTeacherSchool($one->teacherno)){
                    $updateRow += $this->query->table('team')->where($condition)->update($data);
                }
                else{
                    $info.=$one->studentno.'不是本学院教师，无法修改信息。</br>';
                    $errorRow++;
                    $status=0;
                }
            }
        }
            if (isset($postData["inserted"])) {
                $updated = $postData["inserted"];
                $listUpdated = json_decode($updated);
                foreach ($listUpdated as $one) {
                    $data = null;
                    $data['teacherno'] = $one->teacherno;
                    $data['team'] = $one->team;
                    $data['leader'] = $one->leader;
                    if(MyAccess::checkTeacherSchool($one->teacherno)){
                        $row = $this->query->table('team')->insert($data);
                        self::revokeRole($one->teacherno,$one->team);
                        self::addRole($one->teacherno,$one->team);
                        if ($row > 0)
                            $insertRow++;
                    }
                    else{
                        $info.=$one->teacherno.'不是本学院教师，无法添加到团队。</br>';
                        $errorRow++;
                        $status=0;
                    }
                }
            }

            if (isset($postData["deleted"])) {
                $updated = $postData["deleted"];
                $listUpdated = json_decode($updated);
                foreach ($listUpdated as $one) {
                    $condition = null;
                    $condition['teacherno']=$one->teacherno;
                    $condition['team']=$one->team;
                    if(MyAccess::checkTeacherSchool($one->teacherno)) {
                        $deleteRow += $this->query->table('team')->where($condition)->delete();
                        self::revokeRole($one->teacherno,$one->team);
                    }
                    else{
                        $info.=$one->teacherno.'不是本学院教师，无法删除</br>';
                        $errorRow++;
                        $status=0;
                    }
                }
            }
         }
         catch(\Exception $e){
             throw $e;
         }
        if($updateRow+$deleteRow+$insertRow+$errorRow==0){
            $status=0;
            $info="没有数据更新";
        }
        else {
            if ($updateRow > 0) $info .= $updateRow . '条更新！</br>';
            if ($deleteRow > 0) $info .= $deleteRow . '条删除！</br>';
            if ($insertRow > 0) $info .= $insertRow . '条添加！</br>';
        }
        $result=array('info'=>$info,'status'=>$status);
        return $result;
    }
    private static function  addRole($teacherno,$team){
        $bind=array('TEACHERNO'=>$teacherno,'TEAM'=>$team);
        $sql="UPDATE USERS SET ROLES=RTRIM(ROLES)+:TEAM WHERE TEACHERNO=:TEACHERNO";
        Db::execute($sql,$bind);
    }
    private static function  revokeRole($teacherno,$team){
        $bind=array('TEACHERNO'=>$teacherno,'TEAM'=>$team);
        $sql="UPDATE USERS SET ROLES=REPLACE(ROLES,:TEAM,'') WHERE TEACHERNO=:TEACHERNO";
        Db::execute($sql,$bind);
    }

}